#include "PrecompiledHeader.h"
#include "ILightSourceObject.h"

/*--------------------------------------------------------------------------*/

ILightSourceObject::ILightSourceObject(const NodeCreation& nc) :
	INode(ENodeFlag::NoActive | ENodeFlag::NoChildren, nc.m_parent, nc.m_scene)
{
	AssertLogW(nc.m_flags != 0, "Ignoring flags in ILightSourceObject!");

	this->m_ambient = Color::White;
	this->m_specular = Color::White;
	this->m_diffuse = Color::White;

	RenderTaskDesc desc;
	desc.m_material = NULL;
	desc.m_argument = 1;
	desc.m_flags = ERenderTaskFlag::NoMaterial | ERenderTaskFlag::UserInterface;
	desc.m_priority = 0;
	desc.m_object = this;
	this->AddRenderTask(desc);
}

/*--------------------------------------------------------------------------*/

ILightSourceObject::~ILightSourceObject()
{

}

/*--------------------------------------------------------------------------*/

const Color& ILightSourceObject::GetAmbient() const
{
	return this->m_ambient;
}

/*--------------------------------------------------------------------------*/

void ILightSourceObject::SetAmbient(const Color& nval)
{
	this->m_ambient = nval;
}

/*--------------------------------------------------------------------------*/

const Color& ILightSourceObject::GetDiffuse() const
{
	return this->m_diffuse;
}

/*--------------------------------------------------------------------------*/

void ILightSourceObject::SetDiffuse(const Color& nval)
{
	this->m_diffuse = nval;
}

/*--------------------------------------------------------------------------*/

const Color& ILightSourceObject::GetSpecular() const
{
	return this->m_specular;
}

/*--------------------------------------------------------------------------*/

void ILightSourceObject::SetSpecular(const Color& nval)
{
	this->m_specular = nval;
}

/*--------------------------------------------------------------------------*/

uint ILightSourceObject::TreeCalc()
{
	__super::TreeCalc();

	if (this->GetFlags() & ENodeFlag::NoRender)
	{
		return 0;
	}

	/*--------------------------------------------------------------------------*/

	this->SetTranslation(this->GetRelativePosition());
	this->SetScale(this->GetRadius());
	this->CalcModelMatrices();

	this->BindRenderTask(this->GetSceneInstance()->GetRenderPhase(), 0);

	if (g_Keyboard->GetKeyState(EKey::k_lalt))
	{
		this->BindRenderTask(this->GetSceneInstance()->GetRenderPhase(), 1);
	}

#ifdef VL_RENDER_STATISTICS
	g_Hud->GetStatisticsManager()->IncrementStatistic("ILightSourceObject/Success");
#endif

	return 0;
}

/*--------------------------------------------------------------------------*/

void ILightSourceObject::OnRender(const RenderTask* renderTask, uint camarg)
{
	switch (renderTask->GetArgument())
	{

	case 0:
	{
		this->DrawLightVolume();
		break;
	}

	case 1:
	{
		g_Renderer->DrawLine3D(glm::vec3(-1000.0f, 0.0f, 0.0f), glm::vec3(1000.0f, 0.0f, 0.0f), NULL, Color::Red);
		g_Renderer->DrawLine3D(glm::vec3(0.0f, -1000.0f, 0.0f), glm::vec3(0.0f, 1000.0f, 0.0f), NULL, Color::Red);
		g_Renderer->DrawLine3D(glm::vec3(0.0f, 0.0f, -1000.0f), glm::vec3(0.0f, 0.0f, 1000.0f), NULL, Color::Red);
		ROpenGL::PolygonMode(GL_LINE, GL_LINE);
		this->DrawLightVolume();
		break;
	}

	}
}

/*--------------------------------------------------------------------------*/